<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\BizException;
use App\Http\Controllers\Controller;
use App\Model\Agent;
use App\Model\HomeItem;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class HomeItemController extends Controller
{
    public $agent;

    public function __construct()
    {
        $this->middleware(['admin']);
    }

    public function index(Request $request)
    {
        $params = $request->all();
        $list = HomeItem::with('goods')
            ->where('module_id', $params['module_id'])->orderBy('order_num', 'desc')->orderBy('id', 'desc')->paginate(15);
        return view('admin.homeItem.index', [
            'list' => $list,
            'params' => $params,
            'agents' => Agent::where('status', 1)->pluck('name', 'id')->toArray(),
        ]);
    }

    public function add(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'agent_id' => 'required',
                    'goods_id' => 'required',
                ], [
                    'agent_id.required' => '请选择店铺',
                    'goods_id.required' => '请选择商品',
                ]);

                $params = $request->all();
                HomeItem::create($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            return view('admin.homeItem.add', [
                'agents' => Agent::where('status', 1)->pluck('name', 'id')->toArray(),
            ]);
        }
    }

    public function edit(Request $request)
    {
        if ($request->getMethod() == 'POST') {
            try {
                $this->validate($request, [
                    'agent_id' => 'required',
                    'goods_id' => 'required',
                ], [
                    'agent_id.required' => '请选择店铺',
                    'goods_id.required' => '请选择商品',
                ]);

                $params = $request->all();
                $id = $params['id'];

                if (isset($params['id']))
                    unset($params['id']);
                HomeItem::where('id', $id)->update($params);
                return response()->json(['status' => 1, 'info' => '保存成功']);
            } catch (ValidationException $e) {
                return response()->json(['status' => 0, 'info' => $e->validator->errors()->first()]);
            } catch (BizException $e) {
                return response()->json(['status' => 0, 'info' => $e->getMessage()]);
            }
        } else {
            $row = HomeItem::find($request->input('id'));
            return view('admin.homeItem.edit', [
                'row' => $row,
                'agents' => Agent::where('status', 1)->pluck('name', 'id')->toArray(),
            ]);
        }
    }

    public function del(Request $request)
    {
        HomeItem::where('id', $request->input('id'))->delete();
        return response()->json(['status' => 1, 'info' => '删除成功']);
    }
}